<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="性别" prop="gender">
        <el-input
          v-model="queryParams.gender"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="岗位" prop="postName">
        <el-input
          v-model="queryParams.postName"
          placeholder="请输入岗位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="健康证号" prop="cardNo">
        <el-input v-model="queryParams.cardNo" placeholder="请输入健康证号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="有效期开始时间" prop="expireTimeStart">
        <el-date-picker clearable
          v-model="queryParams.expireTimeStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期结束时间" prop="expireTimeEnd">
        <el-date-picker clearable
          v-model="queryParams.expireTimeEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期结束时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:record:add']">新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:record:edit']">修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:record:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:record:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="岗位" align="center" prop="postName" />
      <el-table-column label="健康证号" align="center" prop="cardNo" />
      <el-table-column label="健康证图片" align="center" prop="cardImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cardImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="有效期开始时间" align="center" prop="expireTimeStart" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTimeStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期结束时间" align="center" prop="expireTimeEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTimeEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="有效期" align="center" prop="expireTime" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.params.expireTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:record:edit']">修改</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:record:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改人员健康档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="用户id" prop="userId" hidden>
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="从业人员" prop="userName">
          <el-input v-model="form.userName" placeholder="请选择从业人员" @focus="openDialog" />
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <!-- <el-input v-model="form.gender" placeholder="请输入性别" /> -->
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option label="男" value="男">
            </el-option>
            <el-option label="女" value="女">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位" />
        </el-form-item>
        <el-form-item label="健康证号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入健康证号" />
        </el-form-item>
        <el-form-item label="健康证图片" prop="cardImage">
          <image-upload v-model="form.cardImage" :limit="1"/>
        </el-form-item>
        <el-form-item label="有效期开始时间" prop="expireTimeStart">
          <el-date-picker clearable v-model="form.expireTimeStart" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择有效期开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期结束时间" prop="expireTimeEnd">
          <el-date-picker clearable v-model="form.expireTimeEnd" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择有效期结束时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="是否删除(0正常 1删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除(0正常 1删除)" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="选择从业人员" :visible.sync="dialogVisible" width="50%" append-to-body>

      <el-form :model="queryParamsChild" ref="queryFormChild" size="small" :inline="true" v-show="showSearch"
        label-width="68px">

        <el-form-item label="姓名" prop="searchQuery">
          <el-input v-model="queryParamsChild.searchQuery" placeholder="请输入姓名" clearable
            @keyup.enter.native="fetchData" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryChild">重置</el-button>
        </el-form-item>

      </el-form>


      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="userId" label="ID" width="50" align="center"></el-table-column>
        <el-table-column prop="nickName" label="姓名" align="center"></el-table-column>
        <el-table-column prop="phonenumber" label="手机号" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="selectEmployee(scope.row.userId, scope.row.nickName)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryParamsChild.currentPage" :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
      </el-pagination>
    </el-dialog>


  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/worker/healthRecord";
import { listUser } from "@/api/system/user";

export default {
  name: "Record",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员健康档案表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        gender: null,
        postName: null,
        cardNo: null,
        cardImage: null,
        expireTimeStart: null,
        expireTimeEnd: null,
        expireTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          // { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
      },
      queryParamsChild: {
        searchQuery: '',
        currentPage: 1,
        pageSize_child: 10
      },
      dialogVisible: false,
      tableData: [],
      totalCount: 0
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人员健康档案列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        gender: null,
        postName: null,
        cardNo: null,
        cardImage: null,
        expireTimeStart: null,
        expireTimeEnd: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员健康档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人员健康档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人员健康档案编号为"' + ids + '"的数据项？').then(function () {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('worker/healthRecord/export', {
        ...this.queryParams
      }, `healthRecord_${new Date().getTime()}.pdf`)
    },
    openDialog() {
      this.dialogVisible = true
      this.fetchData()
    },
    async fetchData() {
      const params = {
        pageNum: this.queryParamsChild.currentPage,
        pageSize: this.queryParamsChild.pageSize_child,
        nickName: this.queryParamsChild.searchQuery,
      }
      listUser(params).then(response => {
        this.tableData = response.rows;
        this.totalCount = response.total;
      }
      );
    },
    handleSizeChange(size) {
      this.queryParamsChild.pageSize_child = size;
      this.fetchData();
    },
    handleCurrentChange(page) {
      this.queryParamsChild.currentPage = page;
      this.fetchData();
    },
    selectEmployee(id, nickName) {
      // 选择某一行数据，并将这一行的id返回到页面data中
      this.form.userName = nickName; // 假设这里只是示例，实际应获取具体名称  
      this.form.userId = id; // 存储选中的 ID  
      this.dialogVisible = false;
    },
    resetQueryChild() {
      this.resetForm("queryFormChild");
      this.fetchData();
    }
  }
};
</script>
